#ifndef __gets_file__
#define __gets_file__


#include <func.h>

typedef struct train_s {
    int length;
    char data[1000];
} train_t;
//int getsfile(int netfd,char *filename);
int epollAdd(int epfd, int fd);
int epollDel(int epfd, int fd);

int recvn(int netfd, void *buf, int length){
    int total = 0;
    char *p = (char *)buf;
    while(total < length){
        int ret = recv(netfd,p+total,length-total,0);
        if(ret == 0){
            return -1;
        }
        total += ret;
    }
    return 0;
}

int sendFile(int netfd,char *filename){
    int fd=open(filename,O_RDWR,0666);
    ERROR_CHECK(fd,-1,"open");

    train_t train;
    bzero(&train,sizeof(train));

    struct stat statbuf;
    fstat(fd,&statbuf);
    train.length=sizeof(statbuf.st_size);
    memcpy(train.data,&statbuf.st_size,train.length);

    send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
    while(1){
        bzero(&train,sizeof(train));
        ssize_t ret = read(fd,train.data,sizeof(train.data));
        if(ret==0){
            break;
        }
        train.length=ret;
        send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);

    }
    close(fd);
    printf("上传完成\n");
    return 0;
}

#endif
